package com.woniuxy.service.hospital.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Emp;
import com.woniuxy.entity.Hospital;
import com.woniuxy.entity.User;
import com.woniuxy.entity.excel.HospitalDTO;
import com.woniuxy.listener.HospitalDataListener;
import com.woniuxy.mapper.EmpMapper;
import com.woniuxy.mapper.HospitalMapper;
import com.woniuxy.mapper.UserMapper;
import com.woniuxy.mapper.custom.CustomHospitalMapper;
import com.woniuxy.mapper.custom.CustomUserMapper;
import com.woniuxy.service.hospital.HospitalService;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private CustomHospitalMapper customHospitalMapper;
    @Autowired
    private HospitalMapper hospitalMapper;
    @Autowired
    private CustomUserMapper userMapper;
    @Autowired
    private EmpMapper empMapper;

    @Override
    public PageInfo<Hospital> findAll(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Hospital> hospitals = customHospitalMapper.selectByExample(null);
        getAdmin(hospitals);
        PageInfo info = new PageInfo(hospitals);
        return info;
    }


    @Override
    public void importHospitalDataFromExcel(InputStream inputStream) {
        EasyExcel.read(inputStream, HospitalDTO.class, new HospitalDataListener(customHospitalMapper)).sheet().doRead();
    }

    @Override
    public List<HospitalDTO> hospitalData() {
        List<Hospital> hospitals = customHospitalMapper.selectByExample(null);
        List<HospitalDTO> hospitalDTO = new ArrayList<>(hospitals.size());
        hospitals.forEach(hospital -> {
            HospitalDTO dto = new HospitalDTO();
            BeanUtils.copyProperties(hospital, dto);
            hospitalDTO.add(dto);
        });
        return hospitalDTO;
    }

    /**
     * 医院信息修改
     * @param hospital
     * @return
     */
    @Override
    @Transactional
    public int update(Hospital hospital) {
        int rows = customHospitalMapper.updateByPrimaryKeySelective(hospital);
        //查询dockuser表如果有该医院id则根据医院id修改empid
        List<Integer> list = customHospitalMapper.findAllDockUserHid();
        for (Integer hId : list) {
            if(hospital.gethId() == hId){
                int rows1 = customHospitalMapper.updateDockUserByHid(hospital.getEmpId(), hospital.gethId());
                return rows;
            }
        }
        //没有就插入
        customHospitalMapper.insertDockUser(hospital.gethId(),hospital.getEmpId());
        return rows;
    }

    @Override
    @Transactional
    public int save(Hospital hospital) {
        int rows = customHospitalMapper.insertSelective(hospital);

//        Integer userId = userMapper.findUserIdByUserNameAndPhone(hospital.getEmpName(), hospital.getEmpPhone());
        int rows1 = customHospitalMapper.insertDockUser(hospital.gethId(),hospital.getEmpId());

        return rows;
    }


    @Override
    public int delete(Integer hId) {
        int rows = customHospitalMapper.deleteByPrimaryKey(hId);
        System.out.println("rows = " + rows);
        return rows;
    }

    @Override
    public List<Hospital> findAllHospital() {
        List<Hospital> allHospital = customHospitalMapper.findAllHospital();
        return allHospital;
    }

    @Override
    public List<Hospital> findLike(String str) {
        str = "%" + str + "%";
        List<Hospital> like = hospitalMapper.findLike(str);
        getAdmin(like);
        return like;
    }

    @Override
    public List<Emp> findAllDockUser() {
        List<Emp> allDockUser = empMapper.findAllDockUser();
        return allDockUser;
    }

    @Override
    public List<Emp> findDockUserByHId(Integer hId) {
        Integer empId = customHospitalMapper.findEmpIdByHid(hId);
        List<Emp> list = new ArrayList<>();
        list.add(empMapper.selectByPrimaryKey(empId));
        return list;
    }

    private void getAdmin(List<Hospital> list) {
        for (Hospital hospital : list) {
            Integer empId = customHospitalMapper.findEmpIdByHid(hospital.gethId());
            if (empId != null) {
                Emp emp = empMapper.selectByPrimaryKey(empId);
                hospital.setEmpPhone(emp.getEmpPhone());
                hospital.setEmpName(emp.getEmpName());
            }
        }
    }

}
